package com.augrain.demo.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.augrain.demo.model.Addr;
import com.augrain.demo.model.User;

/**
 * 测试@InitBinder
 * 
 * @author YaoBiao
 * @date 2021/4/13 17:49
 */
@Controller
@RequestMapping("/test")
public class DataBindController {

    // 绑定变量名字和属性，参数封装进类
    @InitBinder("user")
    public void initBinderUser(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("user.");
    }

    // 绑定变量名字和属性，参数封装进类
    @InitBinder("addr")
    public void initBinderAddr(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("addr.");
    }

    @RequestMapping("/bind")
    @ResponseBody
    public Map<String, Object> test(@ModelAttribute("user") User user, @ModelAttribute("addr") Addr addr) {
        Map<String, Object> map = new HashMap<>();
        map.put("user", user);
        map.put("addr", addr);
        return map;
    }
}
